using B.S.NewMedical.Api.Read.Application.Command.ContentManagement.Department;
using B.S.NewMedical.Api.Read.DTOS.Department;
using B.S.NewMedical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace B.S.NewMedical.Api.Read.Controllers.ContentManagement.Department
{
    /// <summary>
    /// 科室显示控制器
    /// </summary>
    [Route("api/ContentManagement/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        private readonly IMediator _mediator;

        public DepartmentController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取一级科室列表（支持分页、科室名称模糊查询、类型筛选、状态筛选）
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>一级科室分页列表（包含科室名称、级别、状态、上传人、添加时间等）</returns>
        [HttpGet]
        public async Task<ApiPageing<DepartmentDisplayDto>> GetFirstLevelDepartments([FromQuery] DepartmentQueryDto queryDto)
        {
            return await _mediator.Send(new GetFirstLevelDepartmentsCommand(queryDto));
        }

        /// <summary>
        /// 根据一级科室ID获取二级科室列表（支持分页、科室名称模糊查询、类型筛选、状态筛选）
        /// </summary>
        /// <param name="parentId">一级科室ID</param>
        /// <param name="queryDto">查询条件</param>
        /// <returns>二级科室分页列表（包含科室名称、级别、状态、父级科室、上传人、添加时间等）</returns>
        [HttpGet]
        public async Task<ApiPageing<DepartmentDisplayDto>> GetSecondLevelDepartments(int parentId, [FromQuery] DepartmentQueryDto queryDto)
        {
            return await _mediator.Send(new GetSecondLevelDepartmentsCommand(parentId, queryDto));
        }

        /// <summary>
        /// 根据ID获取科室详情
        /// </summary>
        /// <param name="id">科室ID</param>
        /// <returns>科室详情（包含科室名称、级别、状态、父级科室、上传人、添加时间等）</returns>
        [HttpGet("{id}")]
        public async Task<ApiResult<DepartmentDisplayDto>> GetDepartmentById(int id)
        {
            return await _mediator.Send(new GetDepartmentByIdCommand(id));
        }

        /// <summary>
        /// 获取所有一级科室（用于下拉选择）
        /// </summary>
        /// <returns>一级科室列表（简化版本，只包含ID和名称）</returns>
        [HttpGet]
        public async Task<ApiPageing<DepartmentDisplayDto>> GetFirstLevelDepartmentsSimple()
        {
            var queryDto = new DepartmentQueryDto
            {
                PageIndex = 1,
                PageSize = 1000, // 获取所有一级科室用于下拉选择
                IsOnline = true  // 只获取在线状态的科室
            };
            return await _mediator.Send(new GetFirstLevelDepartmentsCommand(queryDto));
        }

    }
}